//
//  LiveStreamUploadRoomViewController.swift
//  live
//
//  Created by melvyn on 2018/8/17.
//  Copyright © 2018 NEET. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class LiveStreamUploadRoomViewController: UIViewController {
    
    @IBOutlet weak var glView: UIView!
    @IBOutlet weak var shutdownButton: UIButton!
    @IBOutlet weak var stackView: UIStackView!
    @IBOutlet weak var statusTextLabel: UIButton!
    @IBOutlet weak var durationTextLabel: UIButton!
    @IBOutlet weak var numberOfVisitorsTextLabel: UIButton!
    @IBOutlet weak var containerView: UIView!
    
    var viewModel: LiveStreamUploadRoomViewModel = LiveStreamUploadRoomViewModel()
    
    override var shouldAutorotate: Bool {
        return false
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .allButUpsideDown
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        LiveSession.default.setGLView(glView)
        
        let shapLayer = CAShapeLayer.init()
        shapLayer.path = UIBezierPath.init(roundedRect: shutdownButton.bounds,
                                           byRoundingCorners: [.topRight, .bottomRight],
                                           cornerRadii: shutdownButton.bounds.size).cgPath
        shutdownButton.layer.mask = shapLayer
        
        isViewDidAppear
            .bind(to: LiveSession.default.rx.isRunning)
            .disposed(by: disposeBag)
        
        viewModel.isStreaming
            .drive(containerView.rx.isHidden)
            .disposed(by: disposeBag)
        
        viewModel.isStreaming
            .map { !$0 }
            .drive(shutdownButton.rx.isHidden)
            .disposed(by: disposeBag)
        
        viewModel.isStreaming
            .map { !$0 }
            .drive(stackView.rx.isHidden)
            .disposed(by: disposeBag)
        
        viewModel.isStreaming
            .map { _ in "直播中" }
            .drive(statusTextLabel.rx.title())
            .disposed(by: disposeBag)
        
        viewModel.timeInterval
            .bind(to: durationTextLabel.rx.title())
            .disposed(by: disposeBag)
        
        shutdownButton.rx.tap
            .bind(to: viewModel.shutdown)
            .disposed(by: disposeBag)
        
        LiveSession.default.rx.state
            .subscribe(onNext: {
                print("Live session state changed: ", $0)
            })
            .disposed(by: disposeBag)
        
        LiveSession.default.rx.outputDEBUG
            .subscribe(onNext: {
                print("Live session did receive debug info: ", $0 )
            })
            .disposed(by: disposeBag)

        LiveSession.default.rx.outputError
            .subscribe(onNext: {
                print("Live session did receive error: ", $0)
            })
            .disposed(by: disposeBag)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destination = segue.destination as? LiveStreamUploadConfigurationViewController {
            destination.viewModel = viewModel
        } else if let destination = segue.destination as? DanmakuViewController {
            destination.viewModel = viewModel.danmakuListViewModel
        } else {
            super.prepare(for: segue, sender: sender)
        }
    }
}
